package com.venus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.venus.entity.Module;
import com.venus.entity.Role;
import com.venus.service.ModuleService;
import com.venus.util.Result;
import com.venus.util.TreeNode;
import com.venus.util.TreeNodeList;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Venus
 * @since 2020-06-08
 */
@RestController
@RequestMapping("/module")
public class ModuleController {
    @Resource
    private ModuleService moduleService;

    /**
     * 根据角色获取模块
     *
     * @param rid
     * @return
     */
    @RequestMapping("/getModByRid")
    public Object getModByRid(Integer rid) {
        List<Module> moduleList = moduleService.getModByRid(rid);
        System.out.println(moduleList);
        List<TreeNode> treeNodeList = TreeNodeList.getTree(moduleList);
        System.out.println(treeNodeList);
        return treeNodeList;
    }

    ;

    /**
     * 查询所有模块
     *
     * @return
     */
    @RequestMapping("/selectAll")
    public Object selectAll() {
        List<Module> moduleList = moduleService.list();
        System.out.println(moduleList);
        List<TreeNode> treeNodeList = TreeNodeList.getTree(moduleList);
        System.out.println(treeNodeList);
        return treeNodeList;
    }

    ;

    /**
     * 查询单个模块
     *
     * @param mid
     * @return
     */
    @RequestMapping("/GetModByMid")
    public Object getModuleById(Integer mid) {
        Module module = moduleService.getById(mid);
        System.out.println(module);

        return new Result(0, "success", module);


    }


    @RequestMapping("/create")
    public Object create(Module module) {
        Module modUp = moduleService.getOne(new QueryWrapper<Module>().eq("mod_name", module.getModName()));
        if (modUp == null) {
            boolean create = moduleService.save(module);
            if (create == true) {
                return new Result(0, "添加成功");
            } else {
                return new Result(1, "添加失败！");
            }
        } else {
            return new Result(2, "名字重复换一个吧！");
        }

    }


    @RequestMapping("/update")
    public Object update(Module module) {
        Module modUp = moduleService.getOne(new QueryWrapper<Module>().eq("mod_name", module.getModName()));

        if (modUp == null) {
            boolean update = moduleService.updateById(module);
            if (update == true) {
                return new Result(0, "修改陈功");
            } else {
                return new Result(1, "修改失败！");
            }

        } else {
            return new Result(2, "名字重复换一个吧！");
        }
    }


    @RequestMapping("/delete")
    public Object delete(Integer mid) {
        boolean del = moduleService.removeById(mid);

        if (del == true) {
            return new Result(0, "删除陈功");
        } else {
            return new Result(1, "删除失败！");
        }
    }


}
